


$("#newUser").on('submit', function () {
    var formData = $(this).serialize();
    $.ajax({
        type: 'post',
        url: '/users',
        data: formData,
        success: function () {
            location.reload();
        },
        error: function () {
            alert('用户添加失败');
        }
    })

    return false;//关闭表单默认的提交行为
});

$("#useredit").on('change', "#avatar", function () {
    var formData = new FormData();
    formData.append('avatar', this.files[0]);
    $.ajax({
        type: 'post',
        url: '/upload',
        data: formData,
        //告诉$.ajax方法不要解析请求参数
        processData: false,
        //告诉$.ajax方法不要设置请求参数的类型
        contentType: false,
        success: function (res) {
            $("#preview").attr('src', res[0].avatar);
            $("#hiddenAvatar").val(res[0].avatar);
        }
    });
});

$.ajax({
    type: 'get',
    url: '/users',
    success: function (res) {
        var html = template('userTpl', {
            datas: res
        });
        $("#userBox").html(html);
    }
});

$("#userBox").on('click', '.edit', function () {
    var id = $(this).attr('data-id');
    $.ajax({
        type: 'get',
        url: "/users/" + id,
        data: id,
        success: function (res) {
            var html = template('editTpl', { res });
            $("#useredit").html(html);
        }
    })
});

$("#useredit").on('submit', '#editUser', function () {
    var dormData = $(this).serialize();
    var id = $(this).attr('data-id');
    $.ajax({
        type: 'put',
        url: '/users/' + id,
        data: dormData,
        success: function (res) {
            location.reload();
        }
    })
    return false;
});

$("#userBox").on("click", '.delete', function () {
    if (confirm("您真的要删除这个用户吗")) {
        var id = $(this).attr('data-id');
        $.ajax({
            type: 'delete',
            url: "/users/" + id,
            success: function () {
                location.reload();
            }
        });
    }

})

var selectAll = $('#selectAll');
selectAll.on('change', function () {
    var status = $(this).prop("checked")
    $("#userBox").find('.delchange').prop('checked', status);
    if (status) {
        $("#deleteMany").css('display', 'inline-block');
    } else {
        $("#deleteMany").css('display', 'none');
    }

});
$("#userBox").on('change', '.delchange', function () {
    var inputs = $("#userBox").find('.delchange');
    if (inputs.length == inputs.filter(':checked').length) {
        selectAll.prop('checked', true);
    } else {
        selectAll.prop('checked', false);
    }
    if (inputs.filter(':checked').length > 0) {
        $("#deleteMany").css('display', 'inline-block');
    } else {
        $("#deleteMany").css('display', 'none');
    }
});

$("#deleteMany").on('click', function () {
    var ids = [];
    var chosed = $("#userBox").find('input').filter(':checked');
    chosed.each(function (index, item) {
        ids.push($(item).attr('data-id'));
    });
    if (confirm('您真的确认批量删除用户吗？')) {
        $.ajax({
            type: 'delete',
            url: '/users/' + ids.join('-'),
            success: function () {
                location.reload();
            }
        })
    }
});
